<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class CateController extends Controller
{
    //分类添加
    public function add(Request $request)
    {   $title = '分类添加';

        //会话控制
        $staff = $request->session()->get('staff');
        //查询分类
//        $data = DB::table('bro_cate') -> get();
//        SELECT *,CONCAT(path,',',id) AS sort_str FROM bro_cate ORDER BY sort_str ASC;
        $data = DB::table('bro_cate')
                -> select('*',DB::raw("CONCAT(path,',',id) AS sort_str"))
                -> orderBy('sort_str','ASC')
                -> get();

        foreach ($data as $k => $v) {
            //拆分成数组
            
            $arr = explode(',',$v -> path);
            //统计数组的元素个数
            $num = count($arr);
            //重复字符集
            $str = str_repeat('--→|',$num -1);
            //修改catename名称
            $data[$k] -> catename = $str.$v -> catename;
        }
        return view('admin.cate.add',['title' => $title,'data' =>$data,'staff'=>$staff]);
    }
    //insert
    public function insert(Request $request)
    {
//        dd($request -> all());
        $data = $request -> except('_token');

        //添加跟分类
        if($data['pid'] == 0)
        {
            $data['path'] = 0;
            //添加操作
            $res = DB::table('bro_cate') -> insert($data);
            //判断
            if($res)
            {
                return redirect('/admin/cate/show') -> with(['success' => '添加成功']);
            }else
            {
                return back() -> with(['success' => '添加失败']);
            }
        }
        else
        {//处理path字段
            $parentPath = DB::table('bro_cate') -> where('id','=',$data['pid']) -> first() -> path;
            //拼path字段
            $data['path'] = $parentPath.','.$data['pid'];

            //添加操作
            $res = DB::table('bro_cate') -> insert($data);
            //判断
            if($res)
            {
                return redirect('/admin/cate/show') -> with(['success' => '添加成功']);
            }else
            {
                return back() -> with(['success' => '添加失败']);
            }
        }

    }

    //show分类列表

    public function show(Request $request)
    {
        $title = '分类列表';

        //会话控制
        $staff = $request->session()->get('staff');
        //查询分类
        

        $data = DB::table('bro_cate AS c1')
            -> leftJoin('bro_cate AS c2','c1.pid','=','c2.id')
            -> select('c1.*',DB::raw("CONCAT(c1.path,',',c1.id) AS sort_str"),'c2.catename AS c_name')
            -> orderBy('sort_str','ASC')
            -> paginate($request -> input('num',5));

//        dd($data);
        foreach ($data as $k => $v) {
            if(empty($v -> c_name))
            {
                $data[$k] -> c_name = '---';
            }


            //拆分成数组
            $arr = explode(',',$v -> path);
            //统计数组的元素个数
            $num = count($arr);
            //重复字符集
            $str = str_repeat('--→|',$num -1);
            //修改catename名称
            $data[$k] -> catename = $str.$v -> catename;
        }

//        $show = DB::table('bro_cate') ->where(function($query) use ($request){
//            $query -> where('catename','like','%'.$request -> input('keywords').'%');
//        }) -> paginate($request -> input('num',5));
       return view('admin.cate.show',['title' => $title,'data' =>$data,'request' => $request -> all(),'staff'=>$staff]);

    }

    //edit编辑
    public function edit(Request $request,$id)
    {
       $title = '分类编辑';

        //会话控制
        $staff = $request->session()->get('staff');
        
        //获取分类
        $data = DB::table('bro_cate') -> where('id','=',$id) -> first();

        //分类排序
        $allData = DB::table('bro_cate')
            -> select('*',DB::raw("CONCAT(path,',',id) AS sort_str"))
            -> orderBy('sort_str','ASC')
            -> get();
        //分类缩进
        foreach ($allData as $k => $v) {
            if(empty($v -> c_name))
            {
                $allData[$k] -> c_name = '---';
            }

            //拆分成数组
            $arr = explode(',',$v -> path);
            //统计数组的元素个数
            $num = count($arr);
            //重复字符串
            $str = str_repeat('--→|',$num -1);
            //修改catename名称
            $allData[$k] -> catename = $str.$v -> catename;
        }

        return view('admin.cate.edit',['title' => $title,'data' => $data,'allData' => $allData,'staff'=>$staff]);


    }

    //update 修改
    public function update(Request $request)
    {
        $data = $request -> except('_token');
//        dd($data);
        //获取父path
        if(!$data['pid'] == 0)
        {
            $parentPath = DB::table('bro_cate') -> where('id','=',$data['pid']) -> first() -> path;
            //处理path
            $data['path'] = $parentPath.','.$data['pid'];
        }
//        //处理path
//        $data['path'] = $parentPath.','.$data['pid'];

        //更新操作
        $res = DB::table('bro_cate') -> where('id','=',$data['id']) -> update($data);
//        dd($data);
        //判断
        if($res)
        {
            return redirect('/admin/cate/show') -> with(['success' =>'更新成功']);
        }else
        {
            return back() -> with(['error' => '更新失败']);
        }
    }

    //delete
    public function delete($id)
    {
        //查询
        $data = DB::table('bro_cate')-> where('pid','=',$id) -> get();
        //判断
        if($data)
        {
            return back() -> with(['info' => '对不起,下面有分类商品,不能删除']);
        }else
        {
            $res = DB::table('bro_cate') -> where('id','=',$id) -> delete();
            if($res)
            {
                return redirect('admin/cate/show') -> with(['success'=>'删除成功']);
            }else
            {
                return back() -> with(['info' => '删除失败']);
            }
        }
    }
    //地柜查询无线分类
    public function getCatesPid($pid)
    {
        $data = DB::table('bro_cate') -> where('pid','=','$pid') -> get();
        $arr = [];
        foreach ($data as $k => $v){
            $v -> sub = $this -> getCatesPid($data -> id);
            $arr[] = $v;
        }
        return $arr;

    }
    public function test()
    {
        dd($this -> getCatesPid());
    }
}
